package br.com.fogas.medicao.activity;

import br.com.fogas.medicao.R;
import br.com.fogas.utils.dialog.DialogAlert;
import br.com.fogas.medicao.activity.helper.LoginHelper;
import br.com.fogas.medicao.application.ApplicationFogas;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.utils.StringUtility;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class LoginActivity extends FragmentActivity {
	
	private LoginHelper helper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		if(ApplicationFogas.getInstance().getConfiguracao() == null) {
			this.helper = new LoginHelper(this);
		} else {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent); 
			finish();
		}
		
		String erroSync = getIntent().getStringExtra(Constants.ERROR_SINCRONIZACAO);
		if(StringUtility.isNotEmpty(erroSync)) {
			DialogAlert.newInstance()
			.setTitle(R.string.dialog_title_alert)
			.setCharMessage(erroSync)
			.showDialog(getSupportFragmentManager());
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(helper != null) {
			helper.getFogasDatabase().fecharConexao();
		}
	}

	public LoginHelper getHelper() {
		return helper;
	}
}